//
//  ViewController.m
//  AVOS-dev
//
//  Created by JonyFang on 16/8/21.
//  Copyright © 2016年 JonyFang. All rights reserved.
//

#import "ViewController.h"

#define DEPENDENT_KEY @"dependent"
#define PROVINCE_KEY  @"Province"
#define CITY_KEY      @"City"
#define NAME_KEY      @"name"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation ViewController

#pragma mark - Life Cycle

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark -

#pragma mark 使用 Arrays 实现一对多关系

#pragma mark 使用 Pointers 实现一对多关系

- (void)fetchCitiesInProvinceByAVQuery
{
    AVObject *GuangDong = [AVObject objectWithoutDataWithClassName:PROVINCE_KEY objectId:@"57b9526aa633bd0066601d62"];
    AVQuery *query = [AVQuery queryWithClassName:CITY_KEY];
    [query whereKey:DEPENDENT_KEY equalTo:GuangDong];
    
    [query findObjectsInBackgroundWithBlock:^(NSArray *cities, NSError *error) {
        for (AVObject *city in cities) {
            NSLog(@"city = %@", city[NAME_KEY]);
        }
    }];
}

- (void)fetchProvinceByAVQuery
{
    AVQuery *query = [AVQuery queryWithClassName:CITY_KEY];
    [query whereKey:NAME_KEY equalTo:@"广州"];
    [query includeKey:DEPENDENT_KEY];
    
    [query findObjectsInBackgroundWithBlock:^(NSArray *citys, NSError *error) {
        for (AVObject *city in citys) {
            AVObject *province = [city objectForKey:DEPENDENT_KEY];
            NSLog(@"province = %@", province[NAME_KEY]);
        }
    }];
}

- (void)fetchProvinceForDongGuan
{
    AVObject *DongGuan = [AVObject objectWithoutDataWithClassName:CITY_KEY objectId:@"57b95bb97db2a2005429c6d9"];
    NSArray *keys = [NSArray arrayWithObject:DEPENDENT_KEY];
    [DongGuan fetchInBackgroundWithKeys:keys block:^(AVObject *object, NSError *error) {
        AVObject *province = [object objectForKey:DEPENDENT_KEY];
        NSLog(@"Province = %@", province[NAME_KEY]);
    }];
}

- (void)saveDongGuan
{
    AVObject *GuangDong = [AVObject objectWithoutDataWithClassName:PROVINCE_KEY objectId:@"57b9526aa633bd0066601d62"];
    AVObject *DongGuan  = [[AVObject alloc] initWithClassName:CITY_KEY];
    [DongGuan setObject:@"东莞" forKey:NAME_KEY];
    [DongGuan setObject:GuangDong forKey:DEPENDENT_KEY];
    [DongGuan saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            NSLog(@"Saved DongGuan~~");
        }
    }];
}

- (void)saveGuangZhou
{
    AVObject *GuangZhou = [[AVObject alloc] initWithClassName:CITY_KEY];
    [GuangZhou setObject:@"广州" forKey:NAME_KEY];
    
    AVObject *GuangDong = [[AVObject alloc] initWithClassName:PROVINCE_KEY];
    [GuangDong setObject:@"广东" forKey:NAME_KEY];
    
    [GuangZhou setObject:GuangDong forKey:DEPENDENT_KEY];
    [GuangZhou saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded)
        {
            NSLog(@"Saved GuangZhou~~");
            
            [GuangDong saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                if (succeeded) {
                    NSLog(@"Saved Guangdong~~");
                }
            }];
        }
    }];
}

#pragma mark - Setup Views

- (UIView *)setupHeaderView:(NSString *)text
{
    UIView *header = [UIView new];
    header.backgroundColor = [UIColor purpleColor];
    header.frame = CGRectMake(0, 0, SCREEN_WIDTH, 40);
    
    UILabel *textLabel = [UILabel new];
    textLabel.textColor = [UIColor whiteColor];
    textLabel.font = [UIFont boldSystemFontOfSize:13.0f];
    textLabel.text = text;
    [textLabel sizeToFit];
    
    [header addSubview:textLabel];
    
    [textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(header.mas_centerY);
        make.left.equalTo(header.mas_left).with.offset(13.0f);
        make.right.equalTo(header.mas_right).with.offset(15.0f);
    }];
    
    return header;
}

- (UITableViewCell *)setupCell:(NSString *)text
{
    UITableViewCell *cell = [UITableViewCell new];
    
    cell.selectionStyle = UITableViewCellAccessoryNone;
    cell.backgroundColor = [UIColor whiteColor];
    
    UILabel *textLabel = [UILabel new];
    textLabel.textColor = [UIColor blackColor];
    textLabel.numberOfLines = 0;
    textLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:13.0f];
    textLabel.text = text;
    [textLabel sizeToFit];
    
    [cell addSubview:textLabel];
    
    [textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(cell.mas_centerY);
        make.left.equalTo(cell.mas_left).with.offset(15.0f);
        make.right.greaterThanOrEqualTo(cell.mas_right).with.offset(15.0f);
    }];
    
    return cell;
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0)
    {
        return 5;
    }
    else if (section == 1)
    {
        return 0;
    }
    
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        if (indexPath.row == 0)
        {
            return [self setupCell:@"Pointers 存储:保存关联对象"];
        }
        else if (indexPath.row == 1)
        {
            return [self setupCell:@"Pointers 存储:关联一云端的对象"];
        }
        else if (indexPath.row == 2)
        {
            return [self setupCell:@"Pointers 查询:查一城市的省份"];
        }
        else if (indexPath.row == 3)
        {
            return [self setupCell:@"Pointers 查询:通过一次查询把省份加到本地"];
        }
        else if (indexPath.row == 4)
        {
            return [self setupCell:@"Pointers 查询:查找一个省下所有城市"];
        }
    }
    else if (indexPath.section == 1)
    {
        
    }
    
    return nil;
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40.0f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.0001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = nil;
    if (section == 0)
    {
        headerView = [self setupHeaderView:@"使用 Pointers 实现一对多关系"];
    }
    else if (section == 1)
    {
        headerView = [self setupHeaderView:@"使用 Arrays 实现一对多关系"];
    }
    
    return headerView;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        if (indexPath.row == 0)
        {
            [self saveGuangZhou];
        }
        else if (indexPath.row == 1)
        {
            [self saveDongGuan];
        }
        else if (indexPath.row == 2)
        {
            [self fetchProvinceForDongGuan];
        }
        else if (indexPath.row == 3)
        {
            [self fetchProvinceByAVQuery];
        }
        else if (indexPath.row == 4)
        {
            [self fetchCitiesInProvinceByAVQuery];
        }
    }
    else if (indexPath.section == 1)
    {
        
    }
}


@end
